/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ComExWeb.ui.beans;

import ComExWeb.util.JsfUtil;
import java.io.Serializable;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

/**
 *
 * @author 
 */
public abstract class BaseJSFBean implements Serializable {

    protected String getCurrentPage() {
        return JsfUtil.getFC().getViewRoot().getViewId();
    }

    protected String getCurrentViewId() {
        String urlPagina = getCurrentPage();
        return urlPagina.split("/")[1].split(".xhtml")[0];
    }

    public FacesContext getFacesContext() {

        return JsfUtil.getFC();

    }

    public ExternalContext getExternalContext() {

        return JsfUtil.getEC();

    }

    /**
     * <p>Return a <code>Map</code> of the application scope attributes
     * for this web application.</p>
     */
    public Map getApplicationMap() {

        return getExternalContext().getApplicationMap();

    }

    /**
     * <p>Return a <code>Map</code> of the request scope attributes for
     * the current request.</p>
     */
    public Map getRequestMap() {

        return getExternalContext().getRequestMap();

    }

    protected Object getSessionVal(String key) {
        return JsfUtil.getSessionVal(key);
    }

    protected Object setSessionVal(String key, Object val) {
        return JsfUtil.setSessionVal(key, val);
    }

    protected Flash getFlash() {
        return JsfUtil.getFlash();
    }

    protected Object getFlashVal(String key) {
        return getFlash().get(key);
    }

    protected void setFlashVal(String key, Object val) {
        getFlash().put(key, val);
    }

    protected void keepMessages() {
        JsfUtil.setFlashVal("keepMessages", "true");
    }

    // --------------------------------------------------------- Message Methods
    /**
     * <p>Enqueue a global <code>FacesMessage</code> (not associated
     * with any particular component) containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_INFO</code>.
     * </p>
     *
     * @param summary Summary text for this message
     */
    protected void info(String summary) {

        JsfUtil.info(summary);

    }

    /**
     * <p>Enqueue a <code>FacesMessage</code> associated with the
     * specified component, containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_INFO</code>.
     * </p>
     *
     * @param component Component with which this message is associated
     * @param summary Summary text for this message
     */
    protected void info(UIComponent component, String summary) {

        JsfUtil.info(component, summary);

    }

    /**
     * <p>Enqueue a global <code>FacesMessage</code> (not associated
     * with any particular componen) containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_WARN</code>.
     * </p>
     *
     * @param summary Summary text for this message
     */
    protected void warn(String summary) {

        JsfUtil.warn(summary);

    }

    /**
     * <p>Enqueue a <code>FacesMessage</code> associated with the
     * specified component, containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_WARN</code>.
     * </p>
     *
     * @param component Component with which this message is associated
     * @param summary Summary text for this message
     */
    protected void warn(UIComponent component, String summary) {

        JsfUtil.warn(component, summary);


    }

    /**
     * <p>Enqueue a global <code>FacesMessage</code> (not associated
     * with any particular componen) containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_ERROR</code>.
     * </p>
     *
     * @param summary Summary text for this message
     */
    protected void error(String summary) {

        JsfUtil.error(summary);

    }

    protected void messageDialogInformacao(String summary,String titulo) {

        JsfUtil.messageDialogInformacao(summary,titulo);

    }


    /**
     * <p>Enqueue a <code>FacesMessage</code> associated with the
     * specified component, containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_ERROR</code>.
     * </p>
     *
     * @param component Component with which this message is associated
     * @param summary Summary text for this message
     */
    protected void error(UIComponent component, String summary) {

        JsfUtil.error(component, summary);

    }

    /**
     * <p>Enqueue a global <code>FacesMessage</code> (not associated
     * with any particular componen) containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_FATAL</code>.
     * </p>
     *
     * @param summary Summary text for this message
     */
    protected void fatal(String summary) {

        JsfUtil.fatal(summary);

    }

    /**
     * <p>Enqueue a <code>FacesMessage</code> associated with the
     * specified component, containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_FATAL</code>.
     * </p>
     *
     * @param component Component with which this message is associated
     * @param summary Summary text for this message
     */
    protected void fatal(UIComponent component, String summary) {

        JsfUtil.fatal(component, summary);

    }
}
